package com.atguigu.gmall.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @Author: zm
 * @Date: 2021/8/27 20:05
 */
@Service
public class PaymentInfoServiceImpl implements PaymentInfoService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @Autowired
    private RabbitService rabbitService;

    /**
     * 保存支付信息
     *
     * @param orderInfo 订单详情
     * @param payType   支付类型
     */
    @Override
    public void savePaymentInfo(OrderInfo orderInfo, String payType) {

        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderInfo.getId());
        wrapper.eq("payment_type", payType);
        Integer integer = paymentInfoMapper.selectCount(wrapper);
        if (integer > 0) {
            return;
        }

        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setPaymentType(payType);
        paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
        paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setSubject(orderInfo.getTradeBody());

        paymentInfoMapper.insert(paymentInfo);
    }

    /**
     * @param outTradeNo
     * @param payType
     * @return
     */
    @Override
    public PaymentInfo getPaymentInfo(String outTradeNo, String payType) {
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("out_trade_no", outTradeNo);
        wrapper.eq("payment_type", payType);
        return paymentInfoMapper.selectOne(wrapper);
    }

    /**
     * @param outTradeNo
     * @param payType
     * @param paramsMap
     */
    @Override
    public void paySuccess(String outTradeNo, String payType, Map<String, String> paramsMap) {

        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("out_trade_no", outTradeNo);
        wrapper.eq("payment_type", payType);
        PaymentInfo paymentInfoQuery = paymentInfoMapper.selectOne(wrapper);
        if ("PAID".equals(paymentInfoQuery.getPaymentStatus()) || "CLOSED".equals(paymentInfoQuery.getPaymentStatus())) {
            return;
        }
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setTradeNo(paramsMap.get("trade_no"));
        paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(paramsMap.toString());

//        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
//        wrapper.eq("out_trade_no", outTradeNo);
//        wrapper.eq("payment_type", payType);
//        paymentInfoMapper.update(paymentInfo, wrapper);

        this.updatePaymentInfo(outTradeNo, payType, paymentInfo);

        // 修改成功后发送信息通知订单模块修改状态
        rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY, MqConst.ROUTING_PAYMENT_PAY, paymentInfoQuery.getOrderId());
    }

    @Override
    public void updatePaymentInfo(String outTradeNo, String payType, PaymentInfo paymentInfo) {
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("out_trade_no", outTradeNo);
        wrapper.eq("payment_type", payType);
        paymentInfoMapper.update(paymentInfo, wrapper);
    }

    // 关闭交易记录
    @Override
    public void closePayment(Long orderId) {
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderId);
        Integer count = paymentInfoMapper.selectCount(wrapper);
        if (null == count || count.intValue() == 0) {
            return;
        }
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setPaymentStatus(PaymentStatus.CLOSED.name());
        paymentInfoMapper.update(paymentInfo, wrapper);
    }


}
